package org.anderground.editor.gui.properties.station;

import org.anderground.core.model.Station;
import org.anderground.core.util.Time;
import org.anderground.core.util.TimeFormatException;
import org.anderground.editor.edits.EditsManager;
import org.anderground.editor.edits.SetScheduleAction;

public class StationScheduleProperty extends AbstractStringStationProperty {

	@Override
	protected String getValue(Station station) {
		String open = (station.getOpenTime() != null) ? station.getOpenTime()
				.toString() : "00:00";
		String close = (station.getCloseTime() != null) ? station
				.getCloseTime().toString() : "00:00";
		String text = open + "-" + close;
		return text;

	}

	@Override
	protected void setValue(Station station, String cellEditorValue) {
		try {
			String[] times = cellEditorValue.split("-", 2);
			String open = times[0];
			String close = times[1];
			EditsManager.getInstance().performAction(
					new SetScheduleAction(station, new Time(open), new Time(
							close)));
		} catch (TimeFormatException e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getPropertyName() {
		return "Schedule";
	}

	@Override
	public boolean isEditable() {
		return true;
	}

}
